package com.diandian.backend.file.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileCheckService {

    private static final Logger logger = LoggerFactory.getLogger(LocalFileService.class);

    /**
     * 校验当前文件格式是否在允许范围内
     * @param extension 文件扩展名，不包含点
     * @return
     */
    public boolean extensionCheck(String extension, String allowedExtension){

        if(extension == null || "".equals(extension)){
            return false;
        }

        if(allowedExtension == null || "".equals(allowedExtension) || "*".equals(allowedExtension)){
            return true;
        }

        return allowedExtension.toLowerCase().contains(extension.toLowerCase());
    }

    /**
     * 校验当前文件大小是否在允许范围内
     * @param file 文件
     * @return
     */
    public boolean fileSizeCheck(MultipartFile file, long maxFileSize){
        if(file == null){
            return false;
        }
        long fileSize = file.getSize() / 1024;
        logger.info("当前文件大小：{}KB", fileSize);
        return fileSize <= maxFileSize;
    }

}
